﻿using System;
using SrBBSPro.Entities;
using SrBBSPro.BaseClasses;
using System.Collections.Generic;
using System.Text;
using SrBBSPro.Models;

namespace SrBBSPro.Services
{
    public class BugService: BaseDoubleService<Bug> 
    {
        public List<Bug> GetBugList(long pid, string kind, out int count, int page)
            => GetLimitList(DbBase.Query<Bug>().Where(b => b.Id ==pid)
                .Where(p => p.No.Contains(kind)).ToList(), out count, page);


        public Response UpdateBug(object obj,long Id,string bugNo)
        {
            var res = new Response();
            try
            {
                var bug = GetDoubleEntity(Id, bugNo);
                bug.SetProp(obj);
                using (var transaction = DbBase.GetTransaction())
                {
                    var reslut = DbBase.Update(bug);
                    if (reslut > 0)
                    {
                        res.IsSuccess = true;
                        res.Message = "操作成功";
                    }
                    else
                    {
                        res.Message = "操作失败";
                    }
                    transaction.Complete();
                }
            }
            catch
            {
                res.Message = "操作失败";
            }
            return res;
        }


        public Response InsertOrUpdate(BugInfoModel model)
        {
            if (model.Status != null)
            {
                var response = new Response();
                try
                {
                    var num = 0;
                    using (var transaction = DbBase.GetTransaction())
                    {
                        var bug = GetDoubleEntity(model.Id, model.No);
                        bug.Status = model.Status;
                        if (model.Status == "3")
                        {
                            bug.ConfirmDay = DateTime.Now.ToString("yyyy/MM/dd");
                            bug.ConfirmUser = bug.Issuer;
                            bug.IssuerName = bug.IssuerName;
                        }
                        num =DbBase.Update(bug);
                        transaction.Complete();
                    }
                    if (num > 0)
                    {
                        response.IsSuccess = true;
                        response.Message = "操作成功";
                    }
                    else
                    {
                        response.Message = "操作失败";
                    }
                }
                catch
                {
                    response.Message = "操作失败";
                }
                return response;   
            }
            else
            {
                model.Status = model.Status ?? "0";
                return base.InsertOrUpdate(model); 
            }
        }

        public Bug GetEmptyBug(long pid, string kind)
        {
            var bug = new Bug();
            var li = DbBase.Fetch<Bug>("SELECT  * FROM BUG WHERE Id=@0 And [NO] LIKE @1", pid, kind + "%");
            bug.Id = pid;
            if (li.Count == 0)
            {
                bug.No = kind + "0001";
            }
            else
            {
                var num =  DbBase.Single<int>("SELECT  MAX(CONVERT(INT, SUBSTRING([NO],3,4))) FROM BUG WHERE Id=@0 And [NO] LIKE @1", pid, kind + "%");
                bug.No = kind+string.Format("{0:D4}", num+1);
            }
            return bug;
        }

        public string GetRole(long pid,string sno)        
             => DbBase.Single<string>("SELECT  role FROM ProMember WHERE Id=@0 And [NO] = @1", pid, sno);

        public Dictionary<string,int> GetBugTotol(long pid, string pro)
        {
            var sql = new StringBuilder();
            sql.Append("SELECT COUNT(*),SUM(CASE WHEN STATUS='3' THEN 1 ELSE 0 END) FROM  BUG WHERE ID=@0 AND SUBSTRING(NO,1,2)=@1 ");
            var li = DbBase.Fetch<int[]>(sql.ToString(), pid,pro);
            var dic = new Dictionary<string, int>();
            //if()
            dic.Add("status", 0);
            dic.Add("totol",li[0][0]);
            dic.Add("confirm", li[0][1]);
            return dic;
        }

             
    }
}